﻿using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using WCYCReader.Pages;

namespace WCYCReader.UserControls
{
    public partial class HelpControl : UserControl
    {
        private Canvas OneCanvas;

        private Canvas TwoCanvas;

        private Canvas ThreeCanvas;

        private DoubleAnimation animation1;

        private DoubleAnimation animation2;

        private DoubleAnimation animation3;

        private int pageIndex;

        private bool canvasClipped;

        private string _BasePageName;

        private Storyboard pageTurnStoryboard;

        private PhoneApplicationPage _BasePage;

       

        public HelpControl()
        {
            this.InitializeComponent();
            base.ManipulationStarted += new EventHandler<ManipulationStartedEventArgs>(this.HelpPage_ManipulationStarted);
            base.ManipulationDelta += new EventHandler<ManipulationDeltaEventArgs>(this.HelpPage_ManipulationDelta);
            base.ManipulationCompleted += new EventHandler<ManipulationCompletedEventArgs>(this.HelpPage_ManipulationCompleted);
            this.pageTurnStoryboard = base.Resources["helpPage"] as Storyboard;
        }

        private void HelpPage_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            e.Handled = true;
            FrameworkElement originalSource = e.OriginalSource as FrameworkElement;
            Point point = e.ManipulationContainer.TransformToVisual(this).Transform(e.ManipulationOrigin);
            double x = e.TotalManipulation.Translation.X;
            if (this.pageIndex <= 0 && x > 0)
            {
                return;
            }
            this.OnTouchFlick(originalSource, point, e.TotalManipulation.Translation.X);
        }

        private void HelpPage_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            e.Handled = true;
            this.OnTouchDrag(e.DeltaManipulation.Translation.X);
        }

        private void HelpPage_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            e.Handled = true;
            object originalSource = e.OriginalSource;
            e.ManipulationContainer.TransformToVisual(this).Transform(e.ManipulationOrigin);
        }

        

        private void MovePage(double from, double to)
        {
            if (this.pageTurnStoryboard.GetCurrentState() != ClockState.Active)
            {
                int actualWidth = (int)this.LayoutRoot.ActualWidth;
                this.animation1.From = new double?(from);
                this.animation1.To = new double?(to);
                this.animation2.From = new double?(from + (double)actualWidth);
                this.animation2.To = new double?(to + (double)actualWidth);
                this.animation3.From = new double?(from + (double)(actualWidth << 1));
                this.animation3.To = new double?(to + (double)(actualWidth << 1));
                this.pageTurnStoryboard.Completed += new EventHandler(this.OnPageTurnAnimationCompleted);
                this.pageTurnStoryboard.Begin();
            }
        }

        private void OnPageTurnAnimationCompleted(object sender, EventArgs e)
        {
            this.pageTurnStoryboard.Completed -= new EventHandler(this.OnPageTurnAnimationCompleted);
            if (this.pageIndex == 3)
            {
                base.Visibility = Visibility.Collapsed;
                //MainPage.xaml为启动页时
                //if (this._BasePageName == "MainPage")
                //{
                //    this._BasePage.ApplicationBar.IsVisible = true;
                //    (this._BasePage as MainPage).RemoveHelp();
                //    return;
                //}
                //(this._BasePage as AboutPage).RemoveHelp();
                //IndexPage.xaml为启动页时
                if (this._BasePageName == "IndexPage")
                {
                    this._BasePage.ApplicationBar.IsVisible = true;
                    (this._BasePage as IndexPage).RemoveHelp();
                    return;
                }
                (this._BasePage as AboutPage).RemoveHelp();
            }
        }

        private void OnTouchDrag(double xDelta)
        {
            if (this.pageIndex <= 0 && xDelta >= 0 || this.pageIndex >= 3 && xDelta <= 0)
            {
                return;
            }
            TranslateTransform renderTransform = this.OneCanvas.RenderTransform as TranslateTransform;
            renderTransform.X = renderTransform.X + xDelta;
            TranslateTransform x = this.TwoCanvas.RenderTransform as TranslateTransform;
            x.X = x.X + xDelta;
            TranslateTransform translateTransform = this.ThreeCanvas.RenderTransform as TranslateTransform;
            translateTransform.X = translateTransform.X + xDelta;
        }

        private void OnTouchFlick(FrameworkElement touchTarget, Point position, double xTotal)
        {
            double num = xTotal;
            if (num < 0)
            {
                if (this.pageIndex >= 3)
                {
                    return;
                }
                HelpControl helpControl = this;
                helpControl.pageIndex = helpControl.pageIndex + 1;
            }
            else if (num <= 0)
            {
                if (this.pageIndex >= 3)
                {
                    return;
                }
                HelpControl helpControl1 = this;
                helpControl1.pageIndex = helpControl1.pageIndex + 1;
            }
            else
            {
                if (this.pageIndex <= 0)
                {
                    return;
                }
                HelpControl helpControl2 = this;
                helpControl2.pageIndex = helpControl2.pageIndex - 1;
            }
            this.MovePage(((TranslateTransform)this.OneCanvas.RenderTransform).X, -(this.LayoutRoot.ActualWidth * (double)this.pageIndex));
        }

        public void RemapCanvasPointers()
        {
            int num = (int)((this.LayoutRoot.ActualWidth == 0 ? 480 : this.LayoutRoot.ActualWidth));
            this.OneCanvas = (Canvas)this.LayoutRoot.Children[0];
            this.TwoCanvas = (Canvas)this.LayoutRoot.Children[1];
            this.ThreeCanvas = (Canvas)this.LayoutRoot.Children[2];
            ImageBrush imageBrush = new ImageBrush()
            {
                ImageSource = new BitmapImage(new Uri("/Resource/Help/help1.jpg", UriKind.Relative))
            };
            this.OneCanvas.Background = imageBrush;
            ImageBrush imageBrush1 = new ImageBrush()
            {
                ImageSource = new BitmapImage(new Uri("/Resource/Help/help2.jpg", UriKind.Relative))
            };
            this.TwoCanvas.Background = imageBrush1;
            ImageBrush imageBrush2 = new ImageBrush()
            {
                ImageSource = new BitmapImage(new Uri("/Resource/Help/help3.jpg", UriKind.Relative))
            };
            this.ThreeCanvas.Background = imageBrush2;
            ((TranslateTransform)this.OneCanvas.RenderTransform).X = 0;
            ((TranslateTransform)this.TwoCanvas.RenderTransform).X = (double)num;
            ((TranslateTransform)this.ThreeCanvas.RenderTransform).X = (double)(num << 1);
            this.OneCanvas.Visibility = Visibility.Visible;
            this.TwoCanvas.Visibility = Visibility.Visible;
            this.ThreeCanvas.Visibility = Visibility.Visible;
            this.animation1 = (DoubleAnimation)this.pageTurnStoryboard.Children[0];
            this.animation2 = (DoubleAnimation)this.pageTurnStoryboard.Children[1];
            this.animation3 = (DoubleAnimation)this.pageTurnStoryboard.Children[2];
            if (!this.canvasClipped)
            {
                RectangleGeometry rectangleGeometry = new RectangleGeometry()
                {
                    Rect = new Rect(0, 0, this.LayoutRoot.ActualWidth, this.LayoutRoot.ActualHeight)
                };
                this.OneCanvas.Clip = rectangleGeometry;
                RectangleGeometry rectangleGeometry1 = new RectangleGeometry()
                {
                    Rect = new Rect(0, 0, this.LayoutRoot.ActualWidth, this.LayoutRoot.ActualHeight)
                };
                this.TwoCanvas.Clip = rectangleGeometry1;
                RectangleGeometry rectangleGeometry2 = new RectangleGeometry()
                {
                    Rect = new Rect(0, 0, this.LayoutRoot.ActualWidth, this.LayoutRoot.ActualHeight)
                };
                this.ThreeCanvas.Clip = rectangleGeometry2;
                this.canvasClipped = true;
            }
        }

        public void SetPhoneApplicationPage(PhoneApplicationPage basePage, string name)
        {
            this.pageIndex = 0;
            this._BasePageName = name;
            this._BasePage = basePage;
            base.Visibility = Visibility.Visible;
            this.RemapCanvasPointers();
        }
    }
}